package org.hk.kg.utils;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public interface Constants {
	public static class ConstantsConfig
	{
		private Log log = LogFactory.getLog(this.getClass());
		Map<String,Map<String,String>> config = new Hashtable<String,Map<String,String>>();
		private static final Pattern GROUP_PATTERN = Pattern.compile("\\[(.*)\\]");
		private static final Pattern VALUE_PATTERN = Pattern.compile("(.*)[:]{1}(.*)");
		public ConstantsConfig()
		{
			this.load();
		}
		
		private void load()
		{
			try {
				InputStream ips = LocalResouce.getConfigFile(Constants.CONSTANTS_CONFIG);
				BufferedReader reader = new BufferedReader(new InputStreamReader(ips));
				String line;
				Map currentMap = null;
				while((line = reader.readLine())!=null)
				{
					Matcher group_matcher = GROUP_PATTERN.matcher(line);
					if(group_matcher.find())
					{
						currentMap = new Hashtable<String, String>();
						config.put(group_matcher.group(1), currentMap);
					}
					else
					{
						Matcher value_matcher = VALUE_PATTERN.matcher(line);
						if(value_matcher.find())
						{
							if(null != currentMap)
							{
								currentMap.put(value_matcher.group(1),value_matcher.group(2));
							}
						}
					}
				}
			} catch (IOException e) {
				log.error("加载常量配置失败",e);
			}
		}

		public Map getGroup(String groupName)
		{
			return config.get(groupName);
		}
		
		public String getValue(String group,String key)
		{
			Map<String,String> m = getGroup(group);
			if(null == m)
			{
				return null;
			}
			return m.get(key);
		}
		
		public void append(String group,Map<String,String> value)
		{
			config.put(group, value);
		}
	};
	public static final ConstantsConfig CONFIG = new ConstantsConfig();
	public static final String UI_CONFIG_RENDERS_TAG = "renders";
	public static final String UI_CONFIG_RENDER_TAG = "render";
	public static final String UI_CONFIG_RENDER_CLASS_ATTR = "class";
		
	
	public static final int DEFAULT_WIDTH = 800;
	public static final int DEFAULT_HEIGHT = 600;
	
	public static final String CONFIG_ROOT = "config",
								PAGES_ROOT = "pages",
								IMAGE_ROOT = "pages/images",
								CUSTOM_IMAGE = "custdata",
								CONSTANTS_CONFIG = "constants_config.conf",
								PAGES_TEMP_ROOT = "temp",
								IMAGE_TEMP_ROOT = "temp",
								UI_CONFIG = "ui_config.xml",
								DB_DEFAULT = "alldata.db",
								DB_CONFIG = "db_config.xml",
								DB_DRIVER = "org.sqlite.JDBC";
	
	
	public static final String UI_CONFIG_ACTIONS_TAG = "actions",
								UI_CONFIG_ACTION_TAG = "action",
								UI_CONFIG_TASKS_TAG = "tasks",
								UI_CONFIG_TASK_TAG = "task",
								UI_CONFIG_TABLE_TAG = "table",
								DB_CONFIG_SQL_TAG = "sql",
								UI_CONFIG_ACTION_ID = "id",
								UI_CONFIG_ACTION_PACKAGE = "package",
								UI_CONFIG_CLASS_ATTR = "class",
								UI_CONFIG_MENUS_TAG = "menus",
								UI_CONFIG_MENU_TAG = "menu",
                                UI_CONFIG_MENU_TXT_ATTR = "text",
                                UI_CONFIG_MENU_ID = "id",
                                UI_CONFIG_MENU_ACTION = "action",
                                DB_CONFIG_DATABASES_TAG = "databases",
                                DB_CONFIG_DATABASE_TAG = "database",
                                DB_CONFIG_NAME_ATTR = "name",
                                DB_CONFIG_FLUSH_ATTR = "flush",
                                DB_CONFIG_TYPE_ATTR = "type",
                                DB_CONGIG_PRIMARY_ATTR = "primary",
                                DB_CONFIG_APPLYNULL_ATTR = "nullable",
								DB_CONFIG_FORCE_ATTR = "force",
	
	
								UI_CONFIG_HREF_ATTR = "href",
								UI_CONFIG_BUTTON_VALUE_ATTR = "value",
								UI_CONFIG_BUTTON_ACTION_ATTR = "action",
								UI_SYS_TARGET_KEY = "_target";
	
	public static final Color UI_ALL_BACKGROUND = Color.WHITE;
	public static final Color UI_STATUS_BACKGROUND = new Color(33,33,0,50);
	
	public static final String COMMON_SUCCESS_ACTION = "common_success",
								COMMON_FAIL_ACTION = "common_fail",
								COMMON_SUCCESS_MSG_TITLE = "msg_title",
								COMMON_FAIL_WINDOW_TITLE = "错误";
	
	public static final String ERROR_SYS = "sys-error";
	
	public static final String MSG_COMMON_PAGE_NULL = "系统未配置[%s],请联系支持人员huaikemars@163.com",
								MSG_COMMON_FILE_NOTEXIST = "所选文件不存在!";
								
	
}
